package model;

//import daos.UsuUsuarioJpaController;
import com.j256.ormlite.dao.BaseDaoImpl;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.db.DatabaseType;
import com.j256.ormlite.db.DatabaseTypeUtils;
import com.j256.ormlite.jdbc.DataSourceConnectionSource;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.support.ConnectionSource;
import daos.UserDao;
import daos.VSecurityDao;
import entities.AccAccess;
import entities.FunFunction;
import entities.UsrUser;
import entities.VSecurity;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.zkoss.zk.ui.util.ForEach;

/**
 *
 * @author JS: Juan Sanjuan
 * This Class is user to load the user Access List 
 * to the system using Spring Security
 */
public class CustomUserDetails implements UserDetailsService
{   

    /**
     * 
     * @param username
     * @return UserDetails to be used by the Spring Security Framework
     * @throws UsernameNotFoundException
     * @throws DataAccessException 
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException
    {
        User objSpringUser = null;
        DataBaseHelper objDBH = new DataBaseHelper();
        //JS: Create the User dao Object
        UserDao objUserDao= new UserDao(objDBH.getDataSourceConnection());
        VSecurityDao objSecurityDao= new VSecurityDao(objDBH.getDataSourceConnection());
        UsrUser objUser = objUserDao.findByLogin(username);
        //JS: look for the Access list of this user if its not null
        if (objUser != null)
        {
            //JS: Al  Access for this user
            List<VSecurity> objAccess= objSecurityDao.findByLogin(username);
            //JS: Access list to send to spring framework
            List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(2);
            for (VSecurity objSingleAccess : objAccess)  
            {
                authList.add(new GrantedAuthorityImpl(objSingleAccess.getFunCode()));
            }
            //authList.add(new GrantedAuthorityImpl("USER"));
            objSpringUser = new User(objUser.getUsrLogin(),
                    objUser.getUsrPassword(), true, true, true, true, authList);
        }
        objDBH.CloseConnection();
        //JS: if no errors user wouldnt be null
        return objSpringUser;
    }
}
